package constant;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;

import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import config.OssConfig;
import lombok.Getter;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

public class OssClient {

    @Getter
    private static OSS ossClient = null;

    @PostConstruct
    public static void createOssClient() {
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(OssConfig.ACCESS_KEY, OssConfig.SECRET_KEY);
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        ossClient = OSSClientBuilder.create()
                .endpoint(OssConfig.ENDPOINT)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(OssConfig.REGION)
                .build();
    }

}
